package org.mebusw.struts.vo;

import java.io.Serializable;

import org.mebusw.struts.action.PlayAction;
import org.mebusw.struts.davinci.bean.PlayerPanel;
import org.mebusw.struts.davincicode.CardHand;
import org.mebusw.struts.davincicode.CardPool;
import org.mebusw.struts.davincicode.DavinciCard;
import org.mebusw.struts.davincicode.GameStatus;
import org.mebusw.struts.davincicode.PlayerState;

/**
 * Core of the game, need to split to VO and GameEngine later.
 * @version $Rev: 51 $
 * @author j1shen
 * @see GameEngine 
 * @see PlayAction
 * @see CardPool
 */
public class GameTable implements Serializable {
	public static final int UNKNOWN_PLAYER = -1;
	
	/**initial card count according to player count*/
	private static final int INIT_SET[] = { 0, 0, 4, 4, 3 };	
	
	private int gid;

	/** how many players in total */
	private int playerCount;

	/** player names */
	private String players[];
	private CardHand cardHands[];

	/** whose turn is it now? */
	private int currPlayer;
	private CardHand cardHand;
	private CardPool cardPool;
	private PlayerState playerStates[];
	private String description;
	private GameStatus gameStatus;

	public GameTable() {

	}

	public void init(int playerCount, String p1, String p2, String p3, String p4, String description) {
		System.out.println("GameTable.init()");

		setPlayerCount(playerCount);
		players = new String[playerCount];
		cardHands = new CardHand[playerCount];
		playerStates = new PlayerState[playerCount];
		cardPool = new CardPool();
		gameStatus = new GameStatus();
		DavinciCard card;

		switch (playerCount) {
		case 2:
			players[0] = p1;
			players[1] = p2;
			break;
		case 3:
			players[0] = p1;
			players[1] = p2;
			players[2] = p3;
			break;
		case 4:
			players[0] = p1;
			players[1] = p2;
			players[2] = p3;
			players[3] = p4;
			break;
		default:
			break;
		}

		for (int i = 0; i < playerCount; i++) {
			playerStates[i] = new PlayerState();
			playerStates[i].setState(PlayerState.WAITING);
			cardHands[i] = new CardHand();
			for (int j = 0; j < INIT_SET[playerCount]; j++) {
				card = getCardPool().randTake();
				card.setOwner(i);
				cardHands[i].insertSort(card);
				
			}
		}
		playerStates[0].setState(PlayerState.TAKING);
		setCurrPlayer(0);
		gameStatus.setState(GameStatus.ONGOING);
		
		this.description = description;
	}

	public int getPlayerId(String username) {
		for (int i = 0; i < getPlayerCount(); i++) {
			if (getPlayers()[i].equals(username))
				return i;
		}
		return UNKNOWN_PLAYER;

	}

	public CardHand getCardHand() {
		return this.cardHand;
	}

	public CardPool getCardPool() {
		return this.cardPool;
	}

	public void setCardPool(CardPool cardPool) {
		this.cardPool = cardPool;
	}

	public void setCardHand(CardHand cardHand) {
		this.cardHand = cardHand;
	}


	public String[] getPlayers() {
		return players;
	}

	public void setPlayers(String[] players) {
		this.players = players;
	}

	public CardHand[] getCardHands() {
		return cardHands;
	}

	public void setCardHands(CardHand[] cardHands) {
		this.cardHands = cardHands;
	}

	public int getGid() {
		return gid;
	}

	public void setGid(int gid) {
		this.gid = gid;
	}

	public int getPlayerCount() {
		return playerCount;
	}

	public void setPlayerCount(int playerNbr) {
		this.playerCount = playerNbr;
	}

	public int getCurrPlayer() {
		return currPlayer;
	}

	public void setCurrPlayer(int currPlayer) {
		this.currPlayer = currPlayer;
	}

	public PlayerState[] getPlayerStates() {
		return playerStates;
	}

	public void setPlayerStates(PlayerState[] playerStates) {
		this.playerStates = playerStates;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public GameStatus getGameStatus() {
		return gameStatus;
	}

	public void setGameStatus(GameStatus gameStatus) {
		this.gameStatus = gameStatus;
	}

}
